package com.cdwm.mrp.controller.api;

import com.cdwm.mrp.common.tools.LayuiResponse;
import com.cdwm.mrp.common.tools.ResultVO;
import com.cdwm.mrp.domain.po.Customer;
import com.cdwm.mrp.service.CustomerSerivce;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * @version v1.0
 * @Author: lyw
 * @Date: 2021/8/3
 */
@Api(tags = "客户信息管理")
@RestController
@RequestMapping("/api")
public class CustomerController {

    @Autowired
    private CustomerSerivce customerSerivce;



    @ApiOperation("保存客户信息")
    @PostMapping("/customer/save")
    public ResultVO<?> saveCustomer(@RequestBody Customer customer) {
        return customerSerivce.save(customer) ? ResultVO.ok(): ResultVO.fail();
    }

    @ApiOperation("删除客户信息")
    @PostMapping("/customer/delete/{id}")
    public ResultVO<?> deleteCustomer(@PathVariable Long id) {

        return customerSerivce.removeById(id) ? ResultVO.ok(): ResultVO.fail();
    }


    /**
     * 批量删除用户
     * @param id
     * @return
     */
    @PostMapping("/customer/batchRemove/{id}")
    public ResultVO<?> batchRemove(@PathVariable  Long[] id){

        boolean removeStatus = customerSerivce.removeByIds(Arrays.asList(id));

        if (removeStatus){
            return  ResultVO.ok();
        }
        return  ResultVO.fail();
    }

    @ApiOperation("修改客户信息")
    @PostMapping("/customer/update")
    public ResultVO<?> updateCustomer(@RequestBody Customer customer) {
        return customerSerivce.updateById(customer) ? ResultVO.ok(): ResultVO.fail();
    }


    @ApiOperation("查询客户信息")
    @GetMapping("/customer/list")
    public LayuiResponse<?> listCustomer(
             Customer customer,
            @RequestParam(required = false,defaultValue = "1") Integer page,
            @RequestParam(required = false,defaultValue = "5") Integer limit
    ) {
        return customerSerivce.getCustomerList(customer,page,limit);
    }

    @ApiOperation("查询客户信息名")
    @GetMapping("/customer/listCustomerName")
    public ResultVO<?> listCustomerName() {
        return ResultVO.ok(customerSerivce.list());
    }
}
